﻿using FAMailUtils;
using FAMailUtils.Data.DAO;
using FAMailUtils.Data.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OnlineBusinesses.Areas.CustomerAdmin
{
    public partial class AdminCustomer_Master : System.Web.UI.MasterPage
    {

        private const string PARAMETER_NAME = "?enc=";
        UserLoginDTO userLogin = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                userLogin = getUserLogin();
                lblAccountEmail.Text = Session["EmailLogin"] + "";
                lblAccountName.Text = userLogin.UserType == UserType.CustomerAccount ? userLogin.Client.ClientName : userLogin.Account.AccountName;


                string avatar = userLogin.Client.CompanyLogo;
                if (string.IsNullOrWhiteSpace(avatar)) avatar = ResolveClientUrl("http://placehold.it/250x80");
                master_avatar2.Attributes["src"] = master_avatar.Attributes["src"] =
                  logo.Attributes["src"] = logo2.Attributes["src"] = avatar;

                string secret = "auto" + Session["EmailLogin"];
                string EncryptedString = Server.UrlEncode(FaMailUtils.QueryStringModule.Encrypt(secret.Trim()));
                string url = "http://famails.com/autodirection.aspx";
                //string url = "http://localhost:51054/autodirection.aspx";
                hplAutoResponder.NavigateUrl = url + PARAMETER_NAME + EncryptedString;

                secret = "broa" + Session["EmailLogin"];
                EncryptedString = Server.UrlEncode(FaMailUtils.QueryStringModule.Encrypt(secret.Trim()));
                hplBroadcast.NavigateUrl = url + PARAMETER_NAME + EncryptedString;

                secret = "clie" + Session["EmailLogin"];
                EncryptedString = Server.UrlEncode(FaMailUtils.QueryStringModule.Encrypt(secret.Trim()));
                hplClients.NavigateUrl = url + PARAMETER_NAME + EncryptedString;

                secret = "cust" + Session["EmailLogin"];
                EncryptedString = Server.UrlEncode(FaMailUtils.QueryStringModule.Encrypt(secret.Trim()));
                hplCustomer.NavigateUrl = url + PARAMETER_NAME + EncryptedString;

                secret = "dash" + Session["EmailLogin"];
                EncryptedString = Server.UrlEncode(FaMailUtils.QueryStringModule.Encrypt(secret.Trim()));
                hplDashboard.NavigateUrl = url + PARAMETER_NAME + EncryptedString;

                secret = "disc" + Session["EmailLogin"];
                EncryptedString = Server.UrlEncode(FaMailUtils.QueryStringModule.Encrypt(secret.Trim()));
                hplDiscount.NavigateUrl = url + PARAMETER_NAME + EncryptedString;

                url = "http://mailbox.famails.com/autodirection.aspx";
                //url = "http://localhost:55411/autodirection.aspx";
                EncryptedString = FaMailUtils.QueryStringModule.Encrypt(Session["EmailLogin"] + "");
                hplMailBox.NavigateUrl = url + PARAMETER_NAME + EncryptedString;

                CheckRole();
            }
        }
        void CheckRole()
        {
            string page = Request.Url.Segments[Request.Url.Segments.Length-1].ToLower();
            if (page == "default.aspx" || page == "profile.aspx")
                return;
            if (userLogin.UserType == UserType.SubAccount)
            {
                if (page == "clientdetail.aspx" || page == "payment.aspx" || page == "packagechange.aspx" || page == "clientdetail.aspx" || page == "confirmation.aspx")
                    Response.Redirect(ResolveUrl("~/login.aspx?url=" + Request.Url.PathAndQuery));
            }
            
                AccountDAO data = new AccountDAO();
                if (!data.CheckRole(userLogin, page))
                    Response.Redirect(ResolveUrl("~/login.aspx?url=" + Request.Url.PathAndQuery));
            
        }

        public UserLoginDTO getUserLogin()
        {
            userLogin = (UserLoginDTO)Session["UserLogin"];
            if (userLogin == null)
                Response.Redirect(ResolveUrl("~/login.aspx?url=" + Request.Url.PathAndQuery));
            else
            {
                if (userLogin.Client.ExpireDate != DateTime.MinValue && userLogin.Client.ExpireDate < DateTime.Now)
                    Response.Redirect(ResolveUrl("~/areas/customeradmin/ClientDetail.aspx"), true);
                else
                    return userLogin;
            }
            return null;
        }
    }
}